﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class SportsGroupDAL
    {
        private const string Waming = "SqlServerDAL.SportsGroupDAL";

        public bool Save(SportsGroupDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@GroupName", SqlDbType.VarChar) {Value = dto.GroupName},
                                         new SqlParameter("@GroupCreator", SqlDbType.Int) {Value = dto.GroupCreator},
                                         new SqlParameter("@GroupTime", SqlDbType.DateTime) {Value = dto.GroupTime},
                                         new SqlParameter("@GroupChannel", SqlDbType.VarChar) {Value = dto.GroupChannel},
                                         new SqlParameter("@GroupLogo", SqlDbType.Int) {Value = dto.GroupLogo},
                                         new SqlParameter("@GroupSummary", SqlDbType.VarChar) {Value = dto.GroupSummary},
                                         new SqlParameter("@GroupTemplateId", SqlDbType.Int)
                                             {Value = dto.GroupTemplateId},
                                         new SqlParameter("@GroupCode", SqlDbType.VarChar) {Value = dto.GroupCode}
                                     };
                if (dto.GroupId == 0)
                {
                    sql =
                        @"insert into SportsGroup(GroupName,GroupCreator,GroupTime,GroupChannel,GroupLogo,GroupSummary,GroupTemplateId,GroupCode
                        ) values(@GroupName,@GroupCreator,@GroupTime,@GroupChannel,@GroupLogo,@GroupSummary,@GroupTemplateId,@GroupCode)";
                }
                else
                {
                    sql =
                        @"update SportsGroup set GroupName=@GroupName,GroupCreator=@GroupCreator,GroupTime=@GroupTime,GroupChannel=@GroupChannel,
                        GroupLogo=@GroupLogo,GroupSummary=@GroupSummary,GroupTemplateId=@GroupTemplateId,GroupCode=@GroupCode where GroupId=@GroupId";
                    parameters.Add(new SqlParameter("@GroupId", SqlDbType.Int) { Value = dto.GroupId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(SportsGroupDto dto)", ex);
                return false;
            }
        }

        public Hashtable FindById(int id)
        {
            try
            {
                var ht = new Hashtable();
                const string sql = "select * from SportsGroup where GroupId=@GroupId";
                var param = new SqlParameter("@GroupId", SqlDbType.Int) { Value = id };
                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql, param))
                {
                    if (red.Read())
                    {
                        ht = SqlHelper.FillHashtable(red);
                    }
                }
                return ht;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".FindById(int id)", ex);
                return null;
            }
        }

        public Hashtable FindOne(string code)
        {
            try
            {
                Hashtable ht = null;
                var parameters = new List<SqlParameter>();
                const string sql = "select top 1 * from SportsGroup";
                var whereSql = "";
                if (!string.IsNullOrEmpty(code))
                {
                    whereSql += " where GroupCode=@GroupCode";
                    parameters.Add(new SqlParameter("@GroupCode", SqlDbType.VarChar) { Value = code });
                }
                using (
                    var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql + whereSql,
                                                      parameters.ToArray()))
                {
                    if (red.Read())
                    {
                        ht = SqlHelper.FillHashtable(red);
                    }
                }
                return ht;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".FindOne(string code)", ex);
                return null;
            }
        }
    }
}
